﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.StandardUI;

namespace QLBH.Category
{
    public partial class AddNhanVienDlg : Form
    {
        public eDlgType DlgType { set; get; }
        public NHANVIEN Model { set; get; }
        public AddNhanVienDlg()
        {
            InitializeComponent();

            Model = new NHANVIEN();
            Model.NVID = Guid.NewGuid();
            Model.NVCODE = CodeHelper.GetNewCode();

            Model.NGAYTAO = DateTime.Now;
            Model.NGAYSINH = DateTime.Now;
        }

        private void AddNhanVienDlg_Load(object sender, EventArgs e)
        {
            if (DlgType==eDlgType.ADD)
            {
                this.Text = "Thêm mới nhân viên";

                UpdateModelToControl();
            }
            else
            {
                this.Text = "Sửa thông tin nhân viên";

                UpdateModelToControl();

                dtpNgayTao.Enabled = false;
            }
        }

        private void AddNhanVienDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                //Ctrl+S
                btnSave_Click(sender, e);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                try
                {
                    UpdateModelFromControl();
                    if (DlgType == eDlgType.ADD)
                    {
                        Model = NhanVienController.GetInstance().Add(Model);

                        ACOUNT account = new ACOUNT();
                        account.UserName = tbxUserName.Text;
                        account.NHANVIENID = Model.NVID;
                        account.Pass = MD5Helper.GetMD5(tbxUserName.Text);// default pass is the same with username

                        AcountController.GetInstance().Add(account);
                    }
                    else
                    {
                        Model= NhanVienController.GetInstance().Edit(Model);

                        ACOUNT account = AcountController.GetInstance().FindBy(ac => ac.NHANVIENID == Model.NVID).FirstOrDefault();

                        if (account==null)
                        {
                            //add new
                            account = new ACOUNT();
                            account.UserName = tbxUserName.Text;
                            account.NHANVIENID = Model.NVID;
                            account.Pass = MD5Helper.GetMD5(tbxUserName.Text);// default pass is the same with username

                            AcountController.GetInstance().Add(account);
                        }
                        else if (account != null)
                        {
                            //modify
                            account.UserName = tbxUserName.Text;
                            AcountController.GetInstance().Edit(account);
                        }
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "QL Nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void UpdateModelToControl()
        {
            tbxMaNV.Text = Model.NVCODE;
            tbxTenNV.Text = Model.TENNV;
            dtpNgaySinh.Value = (DateTime)Model.NGAYSINH;

            tbxCMTND.Text = Model.CMTND;
            tbxDiDong.Text = Model.DIDONG;
            tbxDienThoai.Text = Model.SDT;
            tbxEmail.Text = Model.EMAIL;

            tbxLuongCB.Text = Model.LUONGCOBAN.ToString();
            rtbGhiChu.Text = Model.GHICHU;
            rtbDiaChi.Text = Model.DIACHI;
            rtbQueQuan.Text = Model.QUEQUAN;
            dtpNgayTao.Value = (DateTime)Model.NGAYTAO;                        
            

            ACOUNT account = AcountController.GetInstance().FindBy(ac => ac.NHANVIENID == Model.NVID).FirstOrDefault();
            if (account!=null)
            {
                tbxUserName.Text = account.UserName;
            }
            
        }
        private void UpdateModelFromControl()
        {
            Model.NVCODE = tbxMaNV.Text;
            Model.TENNV = tbxTenNV.Text;
            Model.NGAYSINH = dtpNgaySinh.Value;
            Model.NGAYTAO = dtpNgayTao.Value;

            Model.CMTND = tbxCMTND.Text;
            Model.DIACHI = rtbDiaChi.Text;
            Model.DIDONG = tbxDiDong.Text;
            Model.EMAIL = tbxEmail.Text;

            Model.GHICHU = rtbGhiChu.Text;
            Model.LUONGCOBAN = Decimal.Parse(tbxLuongCB.Text);
            Model.QUEQUAN = rtbQueQuan.Text;
            Model.SDT = tbxDienThoai.Text;            
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxMaNV.Text, "Mã NV", 14);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK,MessageBoxIcon.Error);
                tbxMaNV.SelectAll();
                tbxMaNV.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxTenNV.Text, "Tên NV", 50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxTenNV.SelectAll();
                tbxTenNV.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckEmailData(tbxEmail.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDienThoai.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxDienThoai.SelectAll();
                tbxDienThoai.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDiDong.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxDiDong.SelectAll();
                tbxDiDong.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDateTime((DateTime)dtpNgaySinh.Value, "Ngày sinh");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpNgaySinh.Select();
                dtpNgaySinh.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDateTime((DateTime)dtpNgayTao.Value, "Ngày tạo");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpNgayTao.Select();
                dtpNgayTao.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(tbxLuongCB.Text, "Lương CB");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxLuongCB.Select();
                tbxLuongCB.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxUserName.Text, "Tên đăng nhập",50);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL nhân viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxUserName.Select();
                tbxUserName.Focus();
                return false;
            }
            if (!AcountController.GetInstance().CheckValidUserName(tbxUserName.Text))
            {
                ShowMessage.Instance.ShowError("Tên đăng nhập:'" + tbxUserName.Text + "' đã tồn tại");                
                tbxUserName.Select();
                tbxUserName.Focus();
                return false;
            }
            return true;
        }
    }
}
